package com.unicom.priceboard.thread;

import java.util.HashMap;

import com.unicom.priceboard.data.hose.PutExecReader;
import com.unicom.priceboard.data.vo.PutExecElement;
import com.unicom.priceboard.util.Constants;
import com.unicom.priceboard.util.DataPath;
import com.unicom.priceboard.util.ReadFileUtil;
import com.unicom.priceboard.util.Util;

public class PutExecThread implements Runnable{
	
	public static HashMap<String ,PutExecElement> mapPutExec=new HashMap<String, PutExecElement>();
	
	//Path of XML file
	private int wait = 60; //seconds
	
	private static String putExecSrcPath = "";
	private static String securityPath = "";
	
	//String list of confirmNo
	public static String strConfirmNo="";
	private int curday=-1;
	private int prevday=-1;
	private boolean reset_putExcFlag=true;
	private PutExecReader parseExce =new PutExecReader();
	
	public PutExecThread() {
		putExecSrcPath = DataPath.basePath + DataPath.putExecSrcPath;
		securityPath = DataPath.basePath + DataPath.securityPath;
		this.wait = Integer.parseInt(DataPath.hoseWait);
	}
	public void run() {
		while(true)	{
			try{ //check reset new day
				 curday=TimerThread.CURRENT_DAY_OF_WEEK;
				 if(curday!=prevday){
					 if(ReadFileUtil.isUpToDate(Util.getCurrentDataPath(securityPath))){
						 resetPutExec();
						 prevday=curday;
					 }					
				 }// the end
				if(TimerThread.CURRENT_DAY_OF_WEEK!=1 && TimerThread.CURRENT_DAY_OF_WEEK!=7) {
					if(TimerThread.CURRENT_TIME>=1030 && TimerThread.CURRENT_TIME<=1115) {
						//set flag for not read file if not restart server out time trading
						reset_putExcFlag=false;
						int marketState=Integer.parseInt(MarketStatusThread.getCurrentMarketState());
						if(ReadFileUtil.isUpToDate(Util.getCurrentDataPath(putExecSrcPath))){
							if(marketState>=Integer.parseInt(Constants.MARKET_STATE_PTD_SESSION) && marketState !=Integer.valueOf(Constants.MARKET_STATE_PRE_OPEN) ) {
								parseExce.parseExec(putExecSrcPath);
							}	
						 }					
					}else if(mapPutExec.size()==0 && reset_putExcFlag) {
						reset_putExcFlag=false;
						int marketState=Integer.parseInt(MarketStatusThread.getCurrentMarketState());
						if(marketState>=Integer.parseInt(Constants.MARKET_STATE_PTD_SESSION)&& marketState !=Integer.valueOf(Constants.MARKET_STATE_PRE_OPEN)) {
							parseExce.parseExec(putExecSrcPath);	
						}
					}
				}else if(mapPutExec.size()==0) {
					parseExce.parseExec(putExecSrcPath);
				}
			}catch(Exception e)	{
			}			
			try {
				Thread.sleep(1000*wait);//wait a period before continue to read
			} catch (Exception e) {
			}			
		}
	}
	
	//reset data
	private void resetPutExec(){
		 mapPutExec.clear();
		 strConfirmNo="";		
	}
	
}
